import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";

export default defineConfig(({ command, mode }) => {
  // 根据环境设置不同的API地址
  const apiTarget =
    mode === "production"
      ? "http://124.220.171.246:8961" // 生产环境API地址
      : "http://localhost:8961"; // 开发环境API地址

  return {
    plugins: [vue()],
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"),
      },
    },
    define: {
      // 定义环境变量
      __API_BASE_URL__: JSON.stringify(
        mode === "production" ? "/api" : apiTarget + "/api"
      ),
    },
    server: {
      port: 3000,
      open: true,
      host: "0.0.0.0",
      proxy: {
        "/api": {
          target: apiTarget,
          changeOrigin: true,
          rewrite: (path) => path,
        },
        "/deepL": {
          target: "https://api-free.deepl.com",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/deepL/, ""),
        },
      },
    },
    build: {
      outDir: "dist",
      assetsDir: "assets",
      sourcemap: false,
      rollupOptions: {
        output: {
          manualChunks: {
            vendor: ["vue", "vue-router"],
            elementPlus: ["element-plus"],
          },
        },
      },
    },
  };
});
